import json
import environ
import os
import sys
import django
from django_redis import get_redis_connection

ROOT_DIR = (
        environ.Path(__file__) - 3
)
sys.path.append(str(ROOT_DIR))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')
django.setup()

from apps.common.account.models import Role, Permission, RolePermissions


role_perms_bulk_create = []

# 校团委的权限初始化
school_manager_role = Role.objects.only('id').get(id=1)
school_manager_perms = [RolePermissions(role=school_manager_role, permission_id=i) for i in
                        (1, 2, 3, 4, 5, 6, 7, 15, 16, 17, 20, 22, 23, 24, 26,
                         30, 31, 32, 37, 40, 45,
                         60, 65, 70, 72, 75, 77, 78, 79, 81, 85, 86, 87, 88, 89, 91)]
role_perms_bulk_create.extend(school_manager_perms)


# 指导老师的权限初始化
teacher_manager_role = Role.objects.only('id').get(id=10)
teacher_manager_perms = [RolePermissions(role=teacher_manager_role, permission_id=i) for i in
                         (20, 21, 23, 30, 35, 36, 39, 45, 50, 51, 53, 59, 65, 66, 68,
                          70, 75, 76, 78, 80, 85, 86, 88, 90)]
role_perms_bulk_create.extend(teacher_manager_perms)


# 会长的权限初始化
chairman_manager_role = Role.objects.only('id').get(id=30)
chairman_manager_perms = [RolePermissions(role=chairman_manager_role, permission_id=i) for i in
                          (20, 21, 23, 24, 25, 35, 36, 39, 40, 41, 42, 43, 50, 51, 53, 54, 55, 56, 57, 65, 66, 68,
                           69, 75, 76, 78, 79, 85, 86, 88, 89)]
role_perms_bulk_create.extend(chairman_manager_perms)

RolePermissions.objects.bulk_create(role_perms_bulk_create, ignore_conflicts=True)

print('初始化成功')
